package com.atguigu.eduorder.service.impl;

import com.atguigu.common.ordervo.CourseOrderWebVo;
import com.atguigu.common.ordervo.UcenterMemberOrder;
import com.atguigu.common.uuid.IdUtils;
import com.atguigu.eduorder.entity.TOrder;
import com.atguigu.eduorder.feign.EduClient;
import com.atguigu.eduorder.feign.UcenterClient;
import com.atguigu.eduorder.mapper.TOrderMapper;
import com.atguigu.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-02-21
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String memberId) {
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        CourseOrderWebVo courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        TOrder order = copyProperties(courseInfoOrder,userInfoOrder);
        baseMapper.insert(order);
        return order.getOrderNo();
    }

    @Override
    public TOrder getByOrderNo(String orderNo) {
        QueryWrapper<TOrder> qw = new QueryWrapper<>();
        qw.eq("order_no",orderNo).last("limit 1");
        return baseMapper.selectOne(qw);
    }

    private TOrder copyProperties( CourseOrderWebVo courseInfoOrder,UcenterMemberOrder userInfoOrder){
        TOrder order = new TOrder();
        order.setOrderNo(IdUtils.getOrderNo()); //订单号
        order.setCourseId(courseInfoOrder.getId());
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(userInfoOrder.getId());
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        return order;
    }
}
